﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace GemBlaster.Mechanics
{
    /// <summary>
    /// Controls interactions with the Game Board.
    /// </summary>
    public class GameController : IGameController
    {
        Canvas HostCanvas;
        IUiView InfoView;
        IUiView IntroView;
        IUiView GameView;
        IUiView _CurrentView;
        UiViewType _CurrentViewType;
        List<Visual> CurrentViewVisuals;

        public GameController(Canvas hostCanvas)
        {
            CurrentViewVisuals = new List<Visual>();
            HostCanvas = hostCanvas;
            _CurrentView = null;
            _CurrentViewType = UiViewType.Loading;
            IntroView = new IntroView();
            InfoView = new InfoView();
            GameView = new GameView();
            IntroView.Init(this);
            InfoView.Init(this);
            GameView.Init(this);
        }

        public void AddVisual(Visual visual)
        {
            CurrentViewVisuals.Add(visual);
            HostCanvas.Children.Add(visual);
        }

        public void RemoveVisual(Visual visual)
        {
            HostCanvas.Children.Remove(visual);
            CurrentViewVisuals.Remove(visual);
        }

        void ClearVisuals()
        {
            while (CurrentViewVisuals.Count > 0)
                RemoveVisual(CurrentViewVisuals[0]);
        }

        public UiViewType CurrentView
        {
            get
            {
                return _CurrentViewType;
            }
            set
            {
                if (_CurrentViewType != value)
                {
                    _CurrentViewType = value;
                    IUiView NewView = null;
                    switch (value)
                    {
                        case UiViewType.Introduction:
                            NewView = IntroView;
                            break;
                        case UiViewType.HowToPlay:
                            NewView = InfoView;
                            break;
                        case UiViewType.Game:
                            NewView = GameView;
                            break;
                    }
                    if (_CurrentView != null)
                        _CurrentView.Deactivate();
                    ClearVisuals();
                    _CurrentView = NewView;
                    if (NewView != null)
                        NewView.Activate();
                }
            }
        }
    }
}
